package com.buddy.learn.algorithm.exam;

/**
 * @author Buddy
 * @date 2021/6/4
 */
public class SolutionXM {


    /**
     * "i   am a  boy"
     =>
     "boy a am i"
     * @param args
     */
    public static void main(String[] args) {

        String s = "i   am a  boy";
        String res = reverse(s);
        System.out.println(res);

    }

    private static String reverse(String s) {
        String[] strs = s.split(" ");
//        Stack<String> stack = new Stack<>();
//        for (int i=0;i<strs.length;i++){
//            String str = strs[i];
//            if (null != str && !"".equals(str)){
//                stack.push(str);
//            }
//        }
        StringBuffer sb = new StringBuffer();
//        while (!stack.isEmpty()){
//            sb.append(stack.pop());
//            sb.append(" ");
//        }

        for (int i=strs.length-1;i>=0;i--){
            String str = strs[i];
            if (str.isEmpty()){
                continue;
            }
            sb.append(str).append(" ");
        }

        return sb.toString().trim();
    }
}
